//
//  LoginViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "LoginViewController.h"
#pragma mark 登录
#pragma mark
@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UIButton *forgetpsdBtn;

@property (strong, nonatomic) IBOutlet UITextField * nameTextField;
@property (strong, nonatomic) IBOutlet UITextField * psdTextField;



@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"登录"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:nil Target:nil Selector:nil];
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"注册" Target:self Selector:@selector(goReg)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark 登录操作
#pragma mark
-(IBAction) login:(id)sender{
    BOOL  verify = [self verify];
    if(verify){
        [AppConfig removeAgent];
        //弱引用
        __weak LoginViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"mobile":self.nameTextField.text,
                                @"passwd":self.psdTextField.text
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_LOGIN completionHandler:^(id responseObject) {
            
            if(responseObject){
                id obj = [responseObject objectForKey:@"agent"];
                Agent *agent = [[Agent alloc] initWithDictionary:obj];
                agent.KFC_AGENT_MOBILE = [responseObject objectForKey:KFC_AGENT_MOBILE];
                agent.KFC_AGENT_PWD = [responseObject objectForKey:KFC_AGENT_PWD];
                agent.KFC_AGENT_UCID = [responseObject objectForKey:KFC_AGENT_UCID];
                [AppConfig setAgent:[agent dictionary]];
            }
            
            [weak_self back];
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
            
            
        }];
        
        [self.opHandlers addObject:operation];
        
    }
    
    
}

-(BOOL) verify{
    NSString *username = self.nameTextField.text;
    NSString *password = self.psdTextField.text;
    
    UIAlertView *alert = nil;
    
    if ( !validateMobile(username) ) {
        alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(password.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    //    if(password.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    
    
    
    return YES ;
}

#pragma mark 忘记密码
#pragma mark
-(IBAction) forgetpsd:(id)sender{
    ForgetPsdViewController *forgetpsd_vc = [[ForgetPsdViewController alloc] init];
    forgetpsd_vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forgetpsd_vc animated:YES];
}

#pragma mark 去注册页面
#pragma mark
- (void) goReg{
    RegViewController *reg_vc = [[RegViewController alloc] init];
    reg_vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:reg_vc animated:YES];
}

#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.nameTextField resignFirstResponder];
    [self.psdTextField resignFirstResponder];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
   [self animateTextField:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
   [self animateTextField:NO];
}

- (void)animateTextField:(BOOL)up {
    //设置动画的名字
    [UIView beginAnimations:@"Animation" context:nil];
    //设置动画的间隔时间
    [UIView setAnimationDuration:0.20];
    //??使用当前正在运行的状态开始下一段动画
    [UIView setAnimationBeginsFromCurrentState: YES];
    //设置视图移动的位移
    float distance = (up ? -50 : 50);
    self.view.frame = CGRectAddY(self.view.frame, distance);
    //设置动画结束
    [UIView commitAnimations];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self hideKeyboard];
}

#pragma mark 返回
#pragma mark
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
